package org.smile.db.dao;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

import org.smile.db.dao.ann.Id;
import org.smile.db.dao.ann.Property;
import org.smile.db.dao.ann.Table;
/**
 * 用来保存注解了的字段信息
 * @author Administrator
 *
 */
public class ConfigContext
{
	
	private static ConfigContext context=new ConfigContext();
	/**
	 * 注解了的字段信息
	 */
	private Map<Class,Map<String,Field>> classFieldMap=new HashMap<Class,Map<String,Field>>(); 
	/**
	 * 类的主键注解信息
	 */
	private Map<Class,Field> idFieldMap=new ConcurrentHashMap<Class,Field>();
	
	public static ConfigContext getInstance(){
		return context;
	}
	private ConfigContext(){}
	
	public Map<String,Field> getDataField(Class c)
	{
		Map<String,Field> fields = classFieldMap.get(c);
		if(fields==null){
			fields=new TreeMap<String,Field>();
			Field[] fieldArray = c.getDeclaredFields();
			for (int i = 0; i < fieldArray.length; i++)
			{
				if (fieldArray[i].getAnnotation(Id.class) != null)
				{
					fields.put(getIdCollmun(fieldArray[i]).toLowerCase(),fieldArray[i]);
				}else if(fieldArray[i].getAnnotation(Property.class) != null){
					fields.put(getFieldCollmun(fieldArray[i]).toLowerCase(),fieldArray[i]);
				}
			}
			classFieldMap.put(c, fields);
		}
		return fields;
	}
	
	public String getFieldCollmun(Field field){
		
		String collname=field.getAnnotation(Property.class).column();
		if(!"".equals(collname)){
			return collname;
		}else{
			return field.getName();
		}
	}
	public  String getIdCollmun(Field field){
		
		String collname=field.getAnnotation(Id.class).column();
		if(!"".equals(collname)){
			return collname;
		}else{
			return field.getName();
		}
	}
	/**
	 * 得到ID字段
	 * @param c
	 * @return
	 */
	public  Field getIdField(Class c)
	{
		Field field = idFieldMap.get(c);
		if(field==null){
			Field[] fieldArray = c.getDeclaredFields();
			for (int i = 0; i < fieldArray.length; i++)
			{
				if (fieldArray[i].getAnnotation(Id.class) != null)
				{
					field=fieldArray[i];
					idFieldMap.put(c.getClass(),field);
					return field;
				}
			}
		}
		return field;
	}
	/**
	 * 一个类的getter方法
	 * @param c
	 * @return
	 */
	public  Map<String,Method> getMethods(Class c){
		PropertyDescriptor[] propertys;
		try {
			propertys = Introspector.getBeanInfo(c).getPropertyDescriptors();
			Map<String,Method> map=new HashMap<String,Method>();
			for(int i=0;i<propertys.length;i++){
				PropertyDescriptor p=propertys[i];
				Method getter=p.getReadMethod();
				map.put(p.getName(), getter);
			}
			return map;
		} catch (IntrospectionException e) {
			throw new RuntimeException(e);
		}
		
	}
	/**
	 * 类的注解了的表格名
	 * @param c
	 * @return
	 */
	public  String getTableName(Class c){
		String tableName="";
		Table ann =(Table) c.getAnnotation(Table.class);
		if(ann!=null){
			tableName=ann.tableName();
		}
		if("".equals(tableName)){
			tableName=c.getSimpleName();
		}
		return tableName;
	}
	
}
